package com.wc.blue_bridge_history.Java14B.E_合并区域;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/11 10:33
 * 有的是可以
 * @description https://www.lanqiao.cn/paper/3872/problem/3538/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 55;
    static int[][] a = new int[N][N];
    static int n;
    static int[] posX = new int[]{-1, 1, 0, 0};
    static int[] posY = new int[]{0, 0, 1, -1};
    static boolean[][] vis = new boolean[N][N];

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        int resA = work();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        int resB = work();
        out.println(resA + resB);
        out.flush();
    }

    static int dfs(int x, int y) {
        int res = 0;
        for (int i = 0; i < 4; i++) {
            int nx = x + posX[i];
            int ny = y + posY[i];
            if (isNotBorder(nx, ny) && a[nx][ny] == 1) {
                a[nx][ny] = 0;
                res += dfs(nx, ny) + 1;
            }
        }
        return res;
    }

    static int work() {
        int res = 0;
        // 左边一列
        for (int i = 1; i <= n; i++) {
            // 左边
            if (a[i][1] == 1) {
                a[i][1] = 0;
                res = Math.max(res, 1 + dfs(i, 1));
            }
            // 上边
            if (a[1][i] == 1) {
                a[1][i] = 0;
                res = Math.max(res, 1 + dfs(1, i));
            }
            // 右边
            if (a[i][n] == 1) {
                a[i][n] = 0;
                res = Math.max(res, 1 + dfs(i, n));
            }
            // 下边
            if (a[n][i] == 1) {
                a[n][i] = 0;
                res = Math.max(res, 1 + dfs(n, i));
            }
        }
        return res;
    }

    static boolean isNotBorder(int x, int y) {
        return x >= 1 && x <= n && y >= 1 && y <= n;
    }
}


class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
